﻿using MLSMooc.BO;
using MLSMooc.DAO.Implement.Login;
using MLSMooc.DAO.Interface.Login;
using MLSMooc.Helper;
using MLSMooc.Service.Implement.Login;
using MLSMooc.Service.Interface.Login;
using MLSMooc.Session;
using MLSMooc.Session.Implement;
using MLSMooc.Session.Model;
using MLSMooc.VO;
using MLSMooc.VO.Request.Login;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Web;
using System.Web.Mvc;

namespace MLSMooc.Controllers
{
    public class LoginController : Controller
    {
        // GET: Login
        private ILoginService loginService = LoginServiceImpl.GetInstance();
        private IUsersDAO usersDAO = UsersDAO.Getinstance();
        protected ResultModel _result = ResultModel.GetNewInstance();

        [HttpPost]
        public async Task<string> SignIn(SignInReq data)
        {
            Task<(ResultModel, UsersBO)> resultTask = loginService.SignIn(data);
            (ResultModel, UsersBO) result = await resultTask;
            _result.RspCode = result.Item1.RspCode;
            _result.RspMsg = result.Item1.RspMsg;

            //登陆不成功
            if (result.Item1.RspCode <= 0 || result.Item2 == null)
                return SerializeHelper.SerializeJson(_result);

            SessionUser sessionUser = new SessionUser()
            {
                Id = result.Item2.Id,
                AdminName = result.Item2.AdminName,
                RoleId = result.Item2.RoleId,
                LinkName = result.Item2.LinkName,
                EncryptPwd=result.Item2.EncryptPwd
            };
            SessionBaseOpt.ClearSession(ConstantHelper.USER);
            //设置Session
            SessionBaseOpt.SetSessionState(ConstantHelper.USER, await SessionUserService.GetInstance().EnSerializeEncryptAsync(sessionUser));

            return SerializeHelper.SerializeJson(_result);
        }

        [HttpPost]
        public async Task<string> LocationSignIn(SignInReq data)
        {
            Task<(ResultModel, UsersBO)> resultTask = loginService.LocationSignIn(data);
            (ResultModel, UsersBO) result = await resultTask;
            _result.RspCode = result.Item1.RspCode;
            _result.RspMsg = result.Item1.RspMsg;

            //登陆不成功
            if (result.Item1.RspCode <= 0 || result.Item2 == null)
                return SerializeHelper.SerializeJson(_result);

            SessionUser sessionUser = new SessionUser()
            {
                Id = result.Item2.Id,
                AdminName = result.Item2.AdminName,
                RoleId = result.Item2.RoleId,
                LinkName = result.Item2.LinkName,
                EncryptPwd=result.Item2.EncryptPwd

            };
            SessionBaseOpt.ClearSession(ConstantHelper.USER);
            //设置Session
            SessionBaseOpt.SetSessionState(ConstantHelper.USER, await SessionUserService.GetInstance().EnSerializeEncryptAsync(sessionUser));

            return SerializeHelper.SerializeJson(_result);
        }

        [HttpGet]
        public Task<RedirectToRouteResult> SignOut() => Task.Run(() =>
        {
            SessionBaseOpt.ClearSession(ConstantHelper.USER);
            return RedirectToAction("Index");
        });

        //获取用户登录明细
        [HttpGet]
        public async Task<string> GetUserLoginNum()
        {
            var res = await usersDAO.GetUserSignInNum();
            return SerializeHelper.SerializeJson(res);
        }

        //登录成功后吧用户添加到登录明细里
        [HttpPost]
        public async Task<string> InsertSignInInfo(UserSginInBO data)
        {
            var res = await usersDAO.InsertLoginSignInInfo(data.Id);

            if (res <= 0)
            {
                _result.RspCode = res;
                _result.RspMsg = "添加失败";
                return SerializeHelper.SerializeJson(_result);
            }
            else
            {
                _result.RspCode = res;
                _result.RspMsg = "添加成功";
                return SerializeHelper.SerializeJson(_result);
            }

        }

        
    }
}